var app = getApp();
Page({

  data: {
    showOrNot: true,
    gongGaolist: null,
    sid: null,
    showModalStatus: false,
    toClasslist: null,
    classlist: null,
  },

  onshow: function () {
    var that = this;
    this.getGongGao(that, that.data.sid);
  },
  onLoad: function (options) {
    console.log(options);
    var that = this;
    if (options.isStudent == "true") {
      this.setData({
        showOrNot: false
      })
    }

    that.setData({
      sid: app.globalData.sId
    })

    this.getGongGao(that, this.data.sid);
    this.getTeacherclass(that, this.data.sid);
  },
  onPullDownRefresh: function () {
    //上拉刷新再次联网取数据
    var that = this;
    this.getGongGao(that, this.data.sid);
  },

  getTeacherclass(that, sid) {
    console.log(that)
    wx.request({
      url: `https://geek-team.xin/class/findClazzBytId`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        tId: sid
      },
      success(res) {
        if (res.data != []) {
          that.setData({
            classlist: res.data
          })
          console.log(res.data)
        }
      },
      fail: (e) => console.log(e)
    })
  },
  getGongGao(that, sid) {
    console.log(that)
    wx.request({
      url: `https://geek-team.xin/message/findByTId`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        sId: sid
      },
      success(res) {
        if (res.data != []) {
          that.setData({
            gongGaolist: res.data
          })
          console.log(res.data)
        }
      },
      fail: (e) => console.log(e)
    })
  },
  submitGongGao(objs) {
    var that = this;
    console.log(objs);
    var title = objs.detail.value.title;
    var content = objs.detail.value.content;
    wx.request({
      url: `https://geek-team.xin/message/set`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        from: this.data.sid,
        toes: this.data.toClasslist,
        content: content,
        title: title,
        type: "1"//代表公告（message）
      },
      success(res) {
        if (res.data == true) {
          app.globalData.socket.emit("setNotice", `{"toes":"${that.data.toClasslist}"}`, (e) => console.log(e))
          console.log(res.data)
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            showModalStatus: false
          });

        }
        that.getGongGao(that, that.data.sid);
      },
      fail: (e) => console.log(e)
    })

  },
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var that = this;
    var arrayObj = new Array(); //创建一个数组     
    that.setData({
      toClasslist: e.detail.value
    })
  },
  util: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });

    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  }


})
